package cn.org.july.weixin.controller;

import cn.org.july.weixin.vo.TemplateVo;
import cn.org.july.weixin.common.bean.template.TemplateDate;
import cn.org.july.weixin.common.bean.template.TemplateMessage;
import cn.org.july.weixin.service.template.TemplateService;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * 模版管理服务
 * Created with IntelliJ IDEA.
 * User:  wanghongjie
 * Date:  2020/9/10 - 06:47
 * <p>
 * Description:
 */
@RestController
@RequestMapping("/wx/template")
public class TemplateController {


    private final TemplateService templateService;

    public TemplateController(TemplateService templateService) {
        this.templateService = templateService;
    }

    /**
     * 设置行业信息
     *
     * @param body 请求body
     * @return
     */
    @PostMapping("/setIndustry")
    public String setIndustry(@RequestBody String body) throws IOException {
        return templateService.setIndustry(body);
    }

    @GetMapping("/getIndustry")
    public String getIndustry() throws IOException {
        return templateService.getIndustry();
    }

    @GetMapping("/getAllPrivateTemplate")
    public String getAllPrivateTemplate() throws IOException {
        return templateService.getAllPrivateTemplate();
    }

    @PostMapping("/send")
    public String sendTemplate(@RequestBody TemplateVo templateDto) throws IOException {
        TemplateMessage message = TemplateMessage.builder().touser(templateDto.getToUser())
                .template_id(templateDto.getTemplateId())
                .url(templateDto.getUrl())
                .data(TemplateDate.of(templateDto.getData().split("##"))).build();
        return templateService.sendTemplate(message);
    }
}